###########################################################################################
###### creating shape file versions of outputs ############################################
###########################################################################################

#load packages:
library(R.utils)
library(sp)
library(sf)

#Main working dir:
MyDir<-paste("/home/jc217070/Maxent",sep="")  #define working directory on HPC
OutDirFin<-file.path(MyDir,"Outputs3_Final")
IndSppOutDir<-paste(OutDirFin, "/Final_Individual_Spp", sep="")

#set project name:
projectName<-"WHOSnakes"


features<-c("Current_SDMfeatureAll_100.shp",
            "Current_SDMfeatureAll_200.shp",
            "Current_SDMfeatureAll_300.shp",
            "Current_SDMfeatureAll_400.shp",
            "Current_SDMfeatureAll_509.shp")
        

for (feature in features){
  
  print(paste(Sys.time(), "adding shapefiles for", feature, sep=" "))
  
  SDMfeature <- st_read(paste(IndSppOutDir,"/",feature,sep=""))
  SDMfeature<-subset(SDMfeature, SDMfeature$TYPE %in% c("Suitability Value Categories","Known Areas of Occurrence"))

  if(which(features==feature)==1){  
    FullFeature<-SDMfeature
  } else{
    st_crs(SDMfeature)<-st_crs(FullFeature)
    FullFeature<-rbind(FullFeature,SDMfeature)
  }
  
}

print(paste(Sys.time(), "saving final feature", sep=" "))
st_write(FullFeature, paste(IndSppOutDir,"/",cat,"_SDMfeatureAll.shp",sep=""),delete_layer=TRUE)



